﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebAppPlanProduccion.Code;
using PlanProd.Contracts;
using WebAppPlanProduccion.Code.Security;
using PlanProd.Contracts.Search;
using Web.Framework;

namespace WebAppPlanProduccion.App
{
    public partial class ReportEdit : BasePage
    {
        public Report reportCurrent
        {
            get
            {
                return (Report)ViewState["report"];
            }
            set
            {
                ViewState["report"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            HasPermissionToSeeMe(PlanProdSecurity.ADMINISTRAR_REPORTES);

            if (!this.IsPostBack)
            {
                if (this.Request.QueryString["Id"] == null)
                {
                    reportCurrent = new Report();
                    reportCurrent.EmpresaId = BaseWebPage.CompanyId;
                }
                else
                {
                    var id = Convert.ToInt32(this.Request.QueryString["Id"]);
                    reportCurrent = WebServiceHelper.GetReports(new SearchReport() { Id = id }).Single();
                }

                LoadCbos();
                Bind();
            }
        }

        private void LoadCbos()
        {
            var servers = WebServiceHelper.GetReportServers(new SearchReportServer());
            UIHelper.LoadCbo(servers, this.cboReportingServer, string.Empty, "Id", "Url");
        }

        protected override void ApplySecurity()
        {            
        }

        private void Bind()
        {
            if (reportCurrent.Id != 0)
            {
                this.txtId.Text = Convert.ToString(reportCurrent.Id);
                this.txtNombre.Text = reportCurrent.Nombre;
                this.txtReportPath.Text = reportCurrent.ReportPath;
                this.txtSecurityKey.Text = reportCurrent.SecurityKey;
                this.txtLocalizationKey.Text = reportCurrent.LocaleKey;
                if (reportCurrent.Orden.HasValue)
                {
                    this.txtOrden.Text = reportCurrent.Orden.Value.ToString();
                }

                this.cboReportingServer.SelectedValue = reportCurrent.ReportingServerId.ToString();
            }
        }

        private void SetData()
        {                       
            reportCurrent.Nombre = this.txtNombre.Text;
            reportCurrent.ReportPath = this.txtReportPath.Text;
            reportCurrent.SecurityKey = this.txtSecurityKey.Text;
            reportCurrent.LocaleKey = this.txtLocalizationKey.Text;
            reportCurrent.Orden = UIHelper.GetIntFromInputText(this.txtOrden.Text);
            reportCurrent.ReportingServerId = UIHelper.GetIntFromInputCbo(cboReportingServer).Value;
        }

        private void Save()
        {
            reportCurrent = WebServiceHelper.SaveReport(reportCurrent);
        }

        private void Validar()
        {
            // TODO
        }

        protected void lnkGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                Validar();
                SetData();
                Save();
                BasePage.ClearReportingConfiguration(this.Application);
                Response.Redirect("/App/ReportList.aspx", true);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkCancelar_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect("/App/ReportList.aspx", true);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }
    }
}